using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityGameFramework.Runtime;
namespace DTFramework.UI
{
    public abstract class UGuiForm : UIFormLogic
    {
        public const int DepthFactor = 100;
        private float FadeTime = 0.3f;

        private Canvas m_CachedCanvas = null;
        private CanvasGroup m_CanvasGroup = null;
        private List<Canvas> m_CachedCanvasContainer = new List<Canvas>();

        public int OriginalDepth { get; private set; }
        public int Depth => m_CachedCanvas.sortingOrder;
        public CanvasGroup CanvasGroup => m_CanvasGroup;
        public float SetFadeTime { set => FadeTime = value; }

        public virtual void IsActive(bool isactive)
        {
            m_CanvasGroup.alpha = isactive ? 1 : 0;
            m_CanvasGroup.blocksRaycasts = isactive ? true : false;
            m_CanvasGroup.interactable = isactive ? true : false;
        }

        public void CloseUIForm(bool ignoreFade)
        {
            StopAllCoroutines();
            if (ignoreFade)
            {
                if (GameEntry.UI.HasUIForm(UIForm.SerialId))
                {
                    GameEntry.UI.CloseUIForm(this.UIForm.SerialId);
                }
            }
            else
            {
                StartCoroutine(CloseUIForm(FadeTime));
            }
        }
        protected override void OnInit(object userData)
        {
            base.OnInit(userData);

            m_CachedCanvas = gameObject.GetOrAddComponent<Canvas>();
            m_CachedCanvas.overrideSorting = true;
            OriginalDepth = m_CachedCanvas.sortingOrder;

            m_CanvasGroup = gameObject.GetOrAddComponent<CanvasGroup>();

            RectTransform transform = GetComponent<RectTransform>();
            transform.anchorMin = Vector2.zero;
            transform.anchorMax = Vector2.one;
            transform.anchoredPosition = Vector2.zero;
            transform.sizeDelta = Vector2.zero;

            gameObject.GetOrAddComponent<GraphicRaycaster>();
        }
        protected override void OnOpen(object userData)
        {
            base.OnOpen(userData);
            // StopAllCoroutines();
            // m_CanvasGroup.alpha = 0f;
            // StartCoroutine(m_CanvasGroup.FadeToAlpha(1f, FadeTime));
        }
        protected override void OnResume()
        {
            base.OnResume();
            // StopAllCoroutines();

            // m_CanvasGroup.alpha = 0f;
            // StartCoroutine(m_CanvasGroup.FadeToAlpha(1f, FadeTime));
        }
        protected override void OnDepthChanged(int uiGroupDepth, int depthInUIGroup)
        {
            int oldDepth = Depth;
            base.OnDepthChanged(uiGroupDepth, depthInUIGroup);
            int deltaDepth = UGuiGroupHelper.DepthFactor * uiGroupDepth + DepthFactor * depthInUIGroup - oldDepth + OriginalDepth;
            GetComponentsInChildren(true, m_CachedCanvasContainer);
            for (int i = 0; i < m_CachedCanvasContainer.Count; i++)
            {
                m_CachedCanvasContainer[i].sortingOrder += deltaDepth;
            }

            m_CachedCanvasContainer.Clear();
        }
        private IEnumerator CloseUIForm(float duration)
        {
            yield return 0;
            yield return m_CanvasGroup.FadeToAlpha(0f, duration);
            GameEntry.UI.CloseUIForm(this.UIForm.SerialId);
        }

        protected override void OnRecycle() => base.OnRecycle();
        protected override void OnPause() => base.OnPause();
        protected override void OnCover() => base.OnCover();
        protected override void OnReveal() => base.OnReveal();
        protected override void OnRefocus(object userData) => base.OnRefocus(userData);
        protected override void OnUpdate(float elapseSeconds, float realElapseSeconds) => base.OnUpdate(elapseSeconds, realElapseSeconds);
        protected override void OnClose(bool isShutdown, object userData) => base.OnClose(isShutdown, userData);
    }
}